/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.cef.command;

import cef.command.Command;
import cef.command.CommandException;
import genomemap.cef.event.DrawAlignmentMap_Evt;
import genomemap.map.AlignmentMap;
import genomemap.map.GenomeMap;

import javax.swing.*;
import java.util.Collection;
import java.util.List;

/**
 * @author Susanta Tewari
 * @since Sep 26, 2011
 */
public class DrawAlignmentMap_Cmd extends Command<DrawAlignmentMap_Evt> {

    private AlignmentMap alignmentMap;

    /**
     * Creates an instance of this command for the supplied event instance.
     *
     *
     * @param event an implementation instance of the specified event interface
     */
    public DrawAlignmentMap_Cmd(final DrawAlignmentMap_Evt event) {
        super(event);
    }

    @Override
    public void executeImpl() throws CommandException {

        final List<? extends GenomeMap> maps = getEvent().getMaps();
        final Collection<String> commonSet   = AlignmentMap.commonsSet(maps);

        if (commonSet.isEmpty()) throw new CommandException("common set is empty");

        alignmentMap = new AlignmentMap(maps, commonSet);
    }

    /**
     *  Provides the alignment map created by this command. This method should should be called
     *  after the command has finished running or be called back by the command via listeners.
     *
     *  @return the alignment map created
     */
    public JComponent getAlignmentMap() {

        checkCmdFinished();

        return alignmentMap.getComponent();
    }
}
